package com.jcl.elasticsearch.thread;

import com.jcl.elasticsearch.core.thread.ResumeCrawlerContext;
import com.jcl.elasticsearch.core.web.SpringContext;
import com.jcl.elasticsearch.resume.logic.RecruitElasticLogic;
import com.jcl.elasticsearch.resume.logic.ResumeCommonLogic;
import com.jcl.elasticsearch.resume.po.ResumeResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ResumeIntoElasticTask
 * @Description TODO
 * @Author yyliu
 * @Date 2018/11/13 17:33
 * @Version 1.0
 **/
public class ResumeIntoElasticTask {

    private static BlockingQueue<Runnable> queue = new LinkedBlockingDeque<>();
    private static ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 4,
            60L, TimeUnit.SECONDS, queue);

    public static void startResumeIntoElasticThread(ResumeCrawlerContext context) {
        ResumeIntoElasticThread thread = new ResumeIntoElasticThread(context);
        pool.execute(thread);
    }

    public static void start(ResumeIntoElasticThread thread) {
        pool.execute(thread);
    }


    public static class ResumeIntoElasticThread implements Runnable {

        private static final Logger logger = LoggerFactory.getLogger(ResumeIntoElasticThread.class);

        @Override
        public void run() {
            logger.debug("insert into es start ...");
            handle();
            logger.debug("insert into es end . ");
        }

        private void handle() {
            Integer resumeId = context.getId();
            ResumeCommonLogic resumeCommonLogic = SpringContext.getBean(ResumeCommonLogic.class);
            Map<String, Object> data = resumeCommonLogic.findOneMap(resumeId, true);
            RecruitElasticLogic recruitElasticLogic = SpringContext.getBean(RecruitElasticLogic.class);
            String result = recruitElasticLogic.addSearchResume(data);
            logger.debug("add elasticsearch result is : {}", result);
        }

        public ResumeIntoElasticThread() {
        }

        public ResumeIntoElasticThread(ResumeCrawlerContext context) {
            this.context = context;
        }

        public ResumeCrawlerContext getContext() {
            return context;
        }

        public void setContext(ResumeCrawlerContext context) {
            this.context = context;
        }

        private ResumeCrawlerContext context;
    }


}
